﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BaseData;
using BaseData.Common;
using ShiziEntity.User;

namespace ShizirDAL.User
{
    /// <summary>
    /// 经纪人用户信息数据访问类
    /// </summary>
    public class UserAgentData
    {
        /// <summary>
        /// 保存用户基类
        /// </summary>
        /// <param name="user">用户实体</param>
        /// <returns>操作结果</returns>
        public bool SaveUserAgent(UserAgent user)
        {
            string sql = user.GetInsertSql();
            int rows = DataConfig.QueryNoRecords(BusinessType.ShiZirWrite, sql);
            return rows > 0;
        }

        /// <summary>
        /// 获取用户信息实体
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns>用户信息实体</returns>
        public UserAgent GetUserAgentEntity(int userID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" userID,userName,cityName,district,serviceComarea,compayName,mobile,telephone,qq,msn,photoUrl,duty,graduateCollege,motto,shopID,shopName,checkStatus,innerNote ");
            strSql.Append(" from UserAgent ");
            strSql.Append(" where userID=" + userID + " ");
            DataTable dt = DataConfig.QueryRecords(BusinessType.ShiZirRead, strSql.ToString());
            UserAgent model = this.GetEntityHelper(dt);
            return model;
        }

        /// <summary>
        /// 从DataTable中得到一个实体对象
        /// </summary>
        private UserAgent GetEntityHelper(DataTable dt)
        {
            UserAgent model = null;
            if (dt != null && dt.Rows.Count > 0)
            {
                model.UserID = TableHelper.GetIntValueFromDr(dt.Rows[0], "UserID");
                model.UserName = dt.Rows[0]["UserName"].ToString();
                model.CityName = dt.Rows[0]["CityName"].ToString();
                model.District = dt.Rows[0]["District"].ToString();
                model.ServiceComarea = dt.Rows[0]["ServiceComarea"].ToString();
                model.CompayName = dt.Rows[0]["CompayName"].ToString();
                model.Mobile = dt.Rows[0]["Mobile"].ToString();
                model.Telephone = dt.Rows[0]["Telephone"].ToString();
                model.Qq = TableHelper.GetLongValueFromDr(dt.Rows[0], "Qq");
                model.Msn = dt.Rows[0]["Msn"].ToString();
                model.PhotoUrl = dt.Rows[0]["PhotoUrl"].ToString();
                model.Duty = dt.Rows[0]["Duty"].ToString();
                model.GraduateCollege = dt.Rows[0]["GraduateCollege"].ToString();
                model.Motto = dt.Rows[0]["Motto"].ToString();
                model.ShopID = TableHelper.GetIntValueFromDr(dt.Rows[0], "ShopID");
                model.ShopName = dt.Rows[0]["ShopName"].ToString();
                model.CheckStatus = TableHelper.GetIntValueFromDr(dt.Rows[0], "CheckStatus");
                model.InnerNote = dt.Rows[0]["InnerNote"].ToString();
            }
            return model;
        }
    }
}